const express = require('express');
const router = express.Router();
const mysql = require('mysql');


// 连接数据库
const conn = mysql.createConnection( {
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'test',
  port: '3306'
});

conn.connect();

/*new*/
router.get('/home', (req, res) => {
  const sql = `SELECT * FROM home`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/category', (req, res) => {
  const sql = `SELECT * FROM category`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

/*new*/
router.get('/newslist', (req, res) => {
  const sql = `SELECT * FROM newslist`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/getImg', (req, res) => {
  const sql = `SELECT * FROM getImg`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/newsinfo/', (req, res) => {

  let nId=req.query.nId;
  const sql = `SELECT * FROM newsinfo WHERE newsinfo_id='${nId}'`;

  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/photoinfo/', (req, res) => {

  let nId=req.query.nId;
  const sql = `SELECT * FROM photoinfo WHERE photo_id='${nId}'`;

  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.post('/comment/:cId', (req, res) => {

  let cId=req.query.cId;
  let comment='comment'+cId;
  const sql = `insert into ${comment}(ID,content) values (0,?)`;
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.content], function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  });
});
/*
router.param('cId', function(req, res, next, id) {
  next();
});
*/
router.get('/getComment/', (req, res) => {

  let cId=req.query.cId;
  let comment='comment'+cId;
  const sql = `SELECT * FROM ${comment} `;

  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/goodsinfo/', (req, res) => {
  let gId=req.query.gId;


  const sql = `SELECT * FROM goodsinfo WHERE ID='${gId}'`;


  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

router.get('/goodsdesc/', (req, res) => {
  let gId=req.query.gId;


  const sql = `SELECT * FROM goodsdesc WHERE ID='${gId}'`;


  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});


router.get('/goodsinfoimg/', (req, res) => {
  let gId=req.query.gId;


  const sql = `SELECT * FROM goodsinfoimg WHERE ID='${gId}'`;


  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});
/*demo*/
var jsonWrite = function(res, ret) {
  if(typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    });
  } else {
    res.json(ret);
  }
};

// 增加用户接口
router.post('/addUser', (req, res) => {
  const sql = `insert into user(id, name, age) values (0, ?, ?)`;
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.username, params.age], function(err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});

router.get('/suoluetu', (req, res) => {
  let cId=req.query.cId;
  let suoluetu='suoluetu'+cId;
  const sql = `SELECT * FROM ${suoluetu}`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});
router.get('/goodslist', (req, res) => {

  const sql = `SELECT * FROM goodslist`;
  conn.query(sql, (err, data) => {
    if (err) {
      console.log(err);
      res.status(500).send('database err').end();
    } else {
      if (data.length == 0) {
        res.status(500).send('no datas').end();
      } else {
        res.send(data);
      }
    }
  });
});

/*demo*/

module.exports = router;
